{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "83c1d5ae",
   "metadata": {},
   "source": [
    "## 通用场景识别"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4e657bb9",
   "metadata": {},
   "source": [
    "### 1.通用文字识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "97c4a16b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.536fc8f1f9c26ac5a448b0283a16dc0f.315360000.1965038126.282335-25946134', 'expires_in': 2592000, 'session_key': '9mzdCPw+Vp2VPw7/Ct8GO7urKhatSQ5ArWjyVdeVTrgjo1JyubV0fGp48rIvTn9vx7syo0Rp6XQok2OriNx3K/60UbacsA==', 'access_token': '24.3842b0f911d93b19e2b596c211405a55.2592000.1652270126.282335-25946134', 'scope': 'public vis-ocr_ocr brain_ocr_scope brain_ocr_general brain_ocr_general_basic vis-ocr_business_license brain_ocr_webimage brain_all_scope brain_ocr_idcard brain_ocr_driving_license brain_ocr_vehicle_license vis-ocr_plate_number brain_solution brain_ocr_plate_number brain_ocr_accurate brain_ocr_accurate_basic brain_ocr_receipt brain_ocr_business_license brain_solution_iocr brain_qrcode brain_ocr_handwriting brain_form brain_ocr_passport brain_ocr_vat_invoice brain_numbers brain_ocr_business_card brain_ocr_train_ticket brain_ocr_taxi_receipt vis-ocr_household_register vis-ocr_vis-classify_birth_certificate vis-ocr_台湾通行证 vis-ocr_港澳通行证 vis-ocr_机动车购车发票识别 vis-ocr_机动车检验合格证识别 vis-ocr_车辆vin码识别 vis-ocr_定额发票识别 vis-ocr_保单识别 vis-ocr_机打发票识别 vis-ocr_行程单识别 brain_ocr_vin brain_ocr_quota_invoice brain_ocr_birth_certificate brain_ocr_household_register brain_ocr_HK_Macau_pass brain_ocr_taiwan_pass brain_ocr_vehicle_invoice brain_ocr_vehicle_certificate brain_ocr_air_ticket brain_ocr_invoice brain_ocr_insurance_doc brain_formula brain_seal brain_ocr_facade brain_ocr_meter brain_doc_analysis brain_ocr_webimage_loc brain_ocr_medical_paper brain_ocr_doc_analysis_office brain_vat_invoice_verification brain_ocr_medical_detail brain_vehicle_registration_certificate brain_ocr_online_taxi_itinerary brain_ocr_multi_idcard brain_ocr_mixed_multi_vehicle brain_ocr_weigth_note brain_ocr_ multiple_invoice wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online', 'session_secret': '68022fc45e311f1884788f04a95076f7'}\n"
     ]
    }
   ],
   "source": [
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=UOGjB8U7FwBofe59Tpk23ad2&client_secret=W8pZk6UCSsFiTnQBoEnDhA5wlf3VUFza'\n",
    "\n",
    "response = requests.get(host)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "dbdd437e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'refresh_token': '25.536fc8f1f9c26ac5a448b0283a16dc0f.315360000.1965038126.282335-25946134',\n",
       " 'expires_in': 2592000,\n",
       " 'session_key': '9mzdCPw+Vp2VPw7/Ct8GO7urKhatSQ5ArWjyVdeVTrgjo1JyubV0fGp48rIvTn9vx7syo0Rp6XQok2OriNx3K/60UbacsA==',\n",
       " 'access_token': '24.3842b0f911d93b19e2b596c211405a55.2592000.1652270126.282335-25946134',\n",
       " 'scope': 'public vis-ocr_ocr brain_ocr_scope brain_ocr_general brain_ocr_general_basic vis-ocr_business_license brain_ocr_webimage brain_all_scope brain_ocr_idcard brain_ocr_driving_license brain_ocr_vehicle_license vis-ocr_plate_number brain_solution brain_ocr_plate_number brain_ocr_accurate brain_ocr_accurate_basic brain_ocr_receipt brain_ocr_business_license brain_solution_iocr brain_qrcode brain_ocr_handwriting brain_form brain_ocr_passport brain_ocr_vat_invoice brain_numbers brain_ocr_business_card brain_ocr_train_ticket brain_ocr_taxi_receipt vis-ocr_household_register vis-ocr_vis-classify_birth_certificate vis-ocr_台湾通行证 vis-ocr_港澳通行证 vis-ocr_机动车购车发票识别 vis-ocr_机动车检验合格证识别 vis-ocr_车辆vin码识别 vis-ocr_定额发票识别 vis-ocr_保单识别 vis-ocr_机打发票识别 vis-ocr_行程单识别 brain_ocr_vin brain_ocr_quota_invoice brain_ocr_birth_certificate brain_ocr_household_register brain_ocr_HK_Macau_pass brain_ocr_taiwan_pass brain_ocr_vehicle_invoice brain_ocr_vehicle_certificate brain_ocr_air_ticket brain_ocr_invoice brain_ocr_insurance_doc brain_formula brain_seal brain_ocr_facade brain_ocr_meter brain_doc_analysis brain_ocr_webimage_loc brain_ocr_medical_paper brain_ocr_doc_analysis_office brain_vat_invoice_verification brain_ocr_medical_detail brain_vehicle_registration_certificate brain_ocr_online_taxi_itinerary brain_ocr_multi_idcard brain_ocr_mixed_multi_vehicle brain_ocr_weigth_note brain_ocr_ multiple_invoice wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online',\n",
       " 'session_secret': '68022fc45e311f1884788f04a95076f7'}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "abe10686",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.3842b0f911d93b19e2b596c211405a55.2592000.1652270126.282335-25946134'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "access_token = response.json()['access_token']\n",
    "access_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c156e074",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result': [{'words': 'Saying Goodbye To Cambridge Again'}, {'words': '再别康桥'}, {'words': '·Very quietly I take my'}, {'words': '·轻轻的我走了，'}, {'words': 'leave'}, {'words': '正如我轻轻的来；'}, {'words': 'As quietly as I came'}, {'words': 'here;'}, {'words': 'Quietly I wave good-bye'}, {'words': '我轻轻的招手，'}, {'words': 'To the rosy clouds in'}, {'words': 'the western sky.'}, {'words': '作别西天的云彩。'}], 'words_result_num': 13, 'log_id': 1513485892304545805}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用文字识别（高精度版）\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('shige.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = access_token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "503c58bf",
   "metadata": {},
   "source": [
    "### 2.网络文字识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "be1d2719",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result': [{'words': '首届“互联网+'}, {'words': '大学生创新创业大赛'}, {'words': '汇报人:卡卡办公时间:202X.09.09'}], 'words_result_num': 3, 'log_id': 1513486921059624564}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "网络图片文字识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/webimage\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('wangluo.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = access_token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de5f5fb5",
   "metadata": {},
   "source": [
    "### 3.二维码识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "bf68f0b2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'codes_result': [{'text': ['希望疫情快快消退'], 'type': 'QR_CODE'}], 'codes_result_num': 1, 'log_id': 1513487674652268887}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "二维码识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/qrcode\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('erweima.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = access_token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40eacdca",
   "metadata": {},
   "source": [
    "## 卡证文字识别  \n",
    "### 1.身份证或者银行卡  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "2fccbfda",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result': {'姓名': {'location': {'top': 39, 'left': 96, 'width': 67, 'height': 25}, 'words': '韦小宝'}, '民族': {'location': {'top': 82, 'left': 197, 'width': 17, 'height': 20}, 'words': '汉'}, '住址': {'location': {'top': 159, 'left': 90, 'width': 219, 'height': 55}, 'words': '北京市东城区景山前街4号紫禁城敬事房'}, '公民身份号码': {'location': {'top': 0, 'left': 0, 'width': 0, 'height': 0}, 'words': ''}, '出生': {'location': {'top': 0, 'left': 0, 'width': 0, 'height': 0}, 'words': ''}, '性别': {'location': {'top': 81, 'left': 95, 'width': 19, 'height': 22}, 'words': '男'}}, 'words_result_num': 6, 'image_status': 'unknown', 'idcard_number_type': 0, 'log_id': 1513489355801421134}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "身份证识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/idcard\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('shenfenzheng.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"id_card_side\":\"front\",\"image\":img}\n",
    "access_token = access_token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f78817ca",
   "metadata": {},
   "source": [
    "## 财务票据文字识别  \n",
    "### 1.增值税发票识别  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "38fc6ca4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result_num': 43, 'words_result': {'AmountInWords': '叁拾圆整', 'InvoiceNumConfirm': '', 'CommodityEndDate': [], 'CommodityVehicleType': [], 'CommodityStartDate': [], 'CommodityPrice': [{'row': '1', 'word': '120.00'}], 'NoteDrawer': '代用名', 'SellerAddress': '芙芜市芙某区某某路某菜号12458788', 'CommodityNum': [{'row': '1', 'word': '1'}], 'SellerRegisterNum': '910024580015571515', 'MachineCode': '', 'Remarks': '', 'SellerBank': '芜芜市芙某银行某菜支行88888888888888', 'CommodityTaxRate': [{'row': '1', 'word': '25%'}], 'TotalTax': '-90.00', 'InvoiceCodeConfirm': '3300083620', 'CheckCode': '3484568921054844080', 'InvoiceCode': '3300083620', 'InvoiceDate': '2016年03月15日', 'PurchaserRegisterNum': '', 'InvoiceTypeOrg': '菜菜增值税普通发票', 'OnlinePay': '', 'Password': '033+*7-*73*>2170608870>2/>097/3/+/86<9><>990498/72132*7+<>931329*894++897/8>+570*5->>6<589/47*019>/306>26+006<89', 'Agent': '否', 'AmountInFiguers': '30.00', 'PurchaserBank': '', 'Checker': '代用名', 'City': '', 'TotalAmount': '120.00', 'CommodityAmount': [{'row': '1', 'word': '120.00'}], 'PurchaserName': '个人', 'CommodityType': [{'row': '1', 'word': '88888KD'}], 'Province': '浙江', 'InvoiceType': '普通发票', 'SheetNum': '第二联：发票联', 'PurchaserAddress': '', 'CommodityTax': [{'row': '1', 'word': '30.00'}], 'CommodityPlateNum': [], 'CommodityUnit': [{'row': '1', 'word': '套'}], 'Payee': '代用名', 'CommodityName': [{'row': '1', 'word': '公司代用名'}], 'SellerName': '某某某某某有限公司', 'InvoiceNum': '01234567'}, 'log_id': 1513490079789531420}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "增值税发票识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/vat_invoice\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('fapiao.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = access_token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7c0d769a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
